package com.cgsd.heyu.ad;

import android.app.Activity;
import android.view.View;
import android.widget.FrameLayout;

import com.android.library.utils.BeanUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.cgsd.heyu.api.AppConfig;
import com.cgsd.heyu.entity.AdContentEntity;
import com.qq.e.ads.banner2.UnifiedBannerADListener;
import com.qq.e.ads.banner2.UnifiedBannerView;
import com.qq.e.comm.util.AdError;

import java.lang.ref.WeakReference;
import java.util.List;


/**
 * Description : banner广告
 * @author  : xiaoxing
 * 时间：2020/3/11 15
 * 邮箱：only_dream136@sina.com
 */
public class AdBanner {
    private UnifiedBannerView bv;
    private TTAdNative mTTAdNative;
    private TTNativeExpressAd mTTAd;

    private FrameLayout mFrameLayout;
    private AdContentEntity adContentEntity;
    private WeakReference<Activity> weakReference;
    private float width;
    private float height;
    private AdListener adListener;
    private int requestMax;

    public AdBanner(Activity activity, AdContentEntity entity, FrameLayout mFrameLayout, float width, float height){
        this(activity,entity,mFrameLayout,width,height,null);
    }

    public AdBanner(Activity activity, AdContentEntity entity, FrameLayout mFrameLayout, float width, float height, AdListener adListener){
        weakReference = new WeakReference<>(activity);
        this.adContentEntity = entity;
        this.mFrameLayout = mFrameLayout;
        this.width = width;
        this.height = height;
        this.adListener = adListener;

        if (AdContentEntity.CHUANSHANJIA.equals(adContentEntity.platformType)) {
            mTTAdNative = TTAdSdk.getAdManager().createAdNative(weakReference.get());
        }

        if (BeanUtils.isEmpty(adContentEntity)) {
            return;
        }
        mFrameLayout.removeAllViews();
        if (AdContentEntity.GUANGDIANTONG.equals(adContentEntity.platformType)) {
            adBannerG();
        } else {
            adBannerC();
        }
    }


    /**
     * 加载广点通广告
     */
    private void adBannerG(){
        if (this.bv != null) {
            mFrameLayout.removeView(bv);
            bv.destroy();
        }
        bv = new UnifiedBannerView(weakReference.get(),adContentEntity.advertisingId, new UnifiedBannerADListener() {
            @Override
            public void onNoAD(AdError adError) {
                if(adListener != null){
                    adListener.onFail();
                }
                requestMax++;
                if (requestMax <= 5){
                    adBannerG();
                }
            }

            @Override
            public void onADReceive() {

            }

            @Override
            public void onADExposure() {
                if(adListener != null){
                    adListener.onSuccess();
                }
            }

            @Override
            public void onADClosed() {

            }

            @Override
            public void onADClicked() {

            }

            @Override
            public void onADLeftApplication() {

            }

            @Override
            public void onADOpenOverlay() {

            }

            @Override
            public void onADCloseOverlay() {

            }
        });
        mFrameLayout.addView(bv);
        bv.loadAD();
    }

    /**
     * 穿山甲banner广告
     */
    private void adBannerC(){
        mFrameLayout.removeAllViews();
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(adContentEntity.advertisingId)
                .setSupportDeepLink(true)
                //请求广告数量为1到3条
                .setAdCount(1)
                //期望模板广告view的size,单位dp
                .setExpressViewAcceptedSize(width,height)
                //这个参数设置即可，不影响模板广告的size
                .setImageAcceptedSize(640,320 )
                .setUserID(AppConfig.AD_USER_ID)
                .build();
        //step5:请求广告，对请求回调的广告作渲染处理
        mTTAdNative.loadBannerExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String message) {
                mFrameLayout.removeAllViews();
                if(adListener != null){
                    adListener.onFail();
                }
                requestMax++;
                if (requestMax <= 5){
                    adBannerC();
                }
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                if (ads == null || ads.size() == 0){
                    return;
                }
                mTTAd = ads.get(0);
//                mTTAd.setSlideIntervalTime(30*1000);//设置轮播间隔 ms,不调用则不进行轮播展示
                bindAdListener(mTTAd);
                //调用render开始渲染广告
                mTTAd.render();
            }
        });
    }

    /**
     * 绑定广告行为
     */

    private void bindAdListener(TTNativeExpressAd ad) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int type) {
            }

            @Override
            public void onAdShow(View view, int type) {
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                if(adListener != null){
                    adListener.onSuccess();
                }
                //在渲染成功回调时展示广告，提升体验
                mFrameLayout.removeAllViews();
                mFrameLayout.addView(view);
            }
        });
    }


    /**
     * 清空缓存
     */
    public void clear(){
        mTTAdNative = null;
        bv = null;
        if (mTTAd != null) {
            //调用destroy()方法释放
            mTTAd.destroy();
        }
        mTTAd = null;
    }
}
